package com.powernode.utils;

import com.github.pagehelper.Constant;
import com.powernode.common.Constants;
import com.powernode.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebMVCUtils {

    //获得request
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    //获得HttpServletResponse
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getResponse();
    }

    //获得HttpSession
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static ServletContext getContext() {
        return getRequest().getServletContext();
    }

    /**
     * 获取session当前用户
     */
    public static User getCurrentUser() {
        User user = (User) getSession().getAttribute(Constants.SESSION_USER_KEY);
        return user;
    }

    /**
     * 获取当前用户名
     */
    public static String getCurrentUserName() {
        return getCurrentUser().getUsername();
    }

}
